//
//  MainCustomTabBar.m
//  SinaWeibo-OC
//
//  Created by apple on 15/11/28.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "MainCustomTabBar.h"
#import "MainTabBarBtn.h"
#import "UIView+Frame.h"

@interface MainCustomTabBar()

/**
 *  当前点击的按钮
 */
@property (nonatomic,weak) MainTabBarBtn *currentBtn;

@property (nonatomic,weak)UIButton *editButton;

/**
 *  记录子控件的数组
 */
@property (nonatomic,strong) NSMutableArray *btnArray;

@end
@implementation MainCustomTabBar

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        [self setUPUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUPUI];
    }
    return self;
}

- (void)setUPUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    //添加撰写微博按钮
    [self addEditbtn];
    
}
- (void)addEditbtn {
    
    //创建撰写按钮
    UIButton *editButton = [[UIButton alloc] init];
    
    //设置一个标记
    editButton.tag = 100;
    
    //设置背景图片
    [editButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    
    [editButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //设置图片
    [editButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    
    [editButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    self.editButton = editButton;
    
    [self addSubview:editButton];
    
}
- (void)addItem:(UITabBarItem *)item
{
    // 1.创建选项卡按钮
    //    UIButton *btn = [[UIButton alloc] init];
    MainTabBarBtn *btn = [[MainTabBarBtn alloc] init];
    
    // 2.设置按钮属性
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn setImage:item.image forState:UIControlStateNormal];
    [btn setImage:item.selectedImage forState:UIControlStateSelected];
    
    // 3.添加监听事件
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchDown];
    
    // 3.添加按钮到当前view
    [self addSubview:btn];
    
    // 4.设置默认选中按钮
    //将按钮添加到数组中
    btn.tag = self.btnArray.count; //第一次的时候因为数组里没有 所以肯定是0
    [self.btnArray addObject:btn];
    
    if (self.btnArray.count == 1) {
        
        [self btnOnClick:btn];
    }
    
}

- (void)btnOnClick:(MainTabBarBtn *)btn
{
    
    //判断代理是否实现方法
    if ([self.delegate respondsToSelector:@selector(switchTabBar:From:To:)]) {
        
        [self.delegate switchTabBar:self From:self.currentBtn.tag To:btn.tag];
    }
    // 取消上一次选中
    self.currentBtn.selected = NO;
    
    // 选中当前点击的按钮
    btn.selected = YES;
    // 记录当前选中的按钮
    self.currentBtn = btn;
    
        [UIView animateWithDuration:0.2 animations:^{
            // 缩小
            // sx / sy 如果是1代表不缩放, 如果大于1代表放大 如果小于1代表缩小
            btn.transform = CGAffineTransformMakeScale(0.4, 0.4);
    
        } completion:^(BOOL finished) {
    
            [UIView animateWithDuration:0.1 animations:^{
                // 放大
                btn.transform = CGAffineTransformMakeScale(1.4, 1.4);
    
            } completion:^(BOOL finished) {
    
                [UIView animateWithDuration:0.1 animations:^{
                    // 还原
                    btn.transform = CGAffineTransformIdentity;
                }];
            }];
        }];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSUInteger index = 0;
    for (UIView *child in self.subviews) {
        
        if (child.tag != 100) {
            
            // 2.计算每个按钮的frame
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            // 判断是否是中间一个按钮
            if (index == 1) {
                
                index++;
                
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
        self.editButton.size = self.editButton.currentBackgroundImage.size;
        
        self.editButton.centerX = self.width * 0.5;
        
        self.editButton.centerY = self.height * 0.5;
        // 如果想做偏移效果 就打开下面两行代码
        self.editButton.y = -10;
        
        [self bringSubviewToFront:self.editButton];
        
    }
}
#pragma mark - 懒加载
- (NSMutableArray*)btnArray {
    
    if (!_btnArray) {
        
        _btnArray = [NSMutableArray array];
    }
    
    return _btnArray;
}
@end
